<template>
  <div>
    <slot></slot>
    <el-col :span="24" class="bottom_btn" v-if="!readonly">
      <el-button icon="el-icon-plus" @click="addItem">{{ btnName }}</el-button>
    </el-col>
  </div>
</template>

<script>
import { deepClone } from "@/components/form-designer/utils/index";
import { mapGetters } from "vuex";
export default {
  name: "sz-form",
  props: {
    btnName: {
      type: String,
      default: "添加"
    },
    // 限制数量
    limitItem: {
      type: Number,
      default: 10
    },
    config: {
      type: Object,
      default: function() {
        return {};
      }
    },
    // 只读
    readonly: {
      type: Boolean,
      default: false
    },
    // 确认调用接口方法
    funcAddItem: {
      type: Function,
      default: () => {
        return new Promise((resolve, reject) => {});
      }
    }
  },
  components: {},
  computed: {
    ...mapGetters({
      formItemChildren: "formItemChildren"
    }),
    children() {
      if (this.formItemChildren.children) {
        return this.formItemChildren.children;
      } else {
        return deepClone(this.config.children);
      }
    }
  },
  data() {
    return {};
  },
  watch: {},
  mounted() {
    this.initData();
  },
  methods: {
    initData() {
      let para = {};
      para.formId = this.config.formId;
      para.children = deepClone(this.config.children);
      this.$store.dispatch("updateFormItemChildren", para);
    },
    // 新增item
    addItem() {
      if (this.funcAddItem) {
        this.funcAddItem(this.config.formId);
        this.$emit("funcAddItem", this.config.formId);
      }
      if (this.children) {
        if (this.children.length >= this.limitItem) {
          this.warning("最多" + this.limitItem + "项");
          return;
        }
        this.drawingItemCopy(
          this.children[this.children.length - 1],
          this.children,
          this.config.formId
        );
      }
    },
    drawingItemCopy(item, parent, formId) {
      let clone = deepClone(item);
      clone = this.createIdAndKey(clone, parent.length);
      parent.push(clone);
      let para = {};
      para.formId = formId;
      para.children = parent;
      this.$store.dispatch("updateFormItemChildren", para);
    },
    createIdAndKey(item, index) {
      if (item.__config__) {
        let config = item.__config__;
        if (config.children) {
          config.renderKey = +new Date(); // 改变renderKey后可以实现强制更新组件
          config.formId = +new Date(); // 改变renderKey后可以实现强制更新组件
          // let titleTem = item.headerTitle.split('(')
          config.children.map(child => {
            let tem = child.__vModel__.split("_");
            if (Number.isInteger(Number.parseInt(tem[tem.length - 1]))) {
              let cIndex = index + 1;
              // item.headerTitle = titleTem[0] +"("+cIndex+")"
              let model = "";
              tem.map((m, j) => {
                if (j !== tem.length - 1) {
                  model += m + "_";
                }
              });
              child.renderKey = +new Date(); // 改变renderKey后可以实现强制更新组件
              child.formId = +new Date(); // 改变renderKey后可以实现强制更新组件
              child.__vModel__ = model + cIndex;
              child.__config__.defaultValue = "";
              console.log(child.__vModel__);
            } else {
              let cIndex = 1;
              // item.headerTitle = titleTem[0] +"("+cIndex+")"
              child.renderKey = +new Date(); // 改变renderKey后可以实现强制更新组件
              child.formId = +new Date(); // 改变renderKey后可以实现强制更新组件
              child.__vModel__ = child.__vModel__ + "_" + cIndex;
              child.__config__.defaultValue = "";
              console.log(child.__vModel__);
            }
          });
        }
      }
      return item;
      console.log(item);
    },
    // 删除item
    delItem() {}
  }
};
</script>

<style scoped></style>
